#!/usr/bin/env bash

ORIGINAL_DIR=$(pwd)
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )"
echo "moving to $SCRIPT_DIR"
cd $SCRIPT_DIR

bun build ./functions/cf-static-site-router-worker/index.ts --target=node --outdir ./dist/cf-static-site-router-worker/
bun build ./functions/cf-ssr-site-router-worker/index.ts --target=node --outdir ./dist/cf-ssr-site-router-worker/
bun build ./functions/nodejs-runtime/index.ts --target=node --outdir ./dist/nodejs-runtime/
bun build ./functions/nodejs-runtime/loop.ts --target=node --outdir ./dist/nodejs-runtime/
GOARCH=amd64 GOOS=linux go build -trimpath -mod=readonly -ldflags="-buildid=" -o ./dist/bridge/bootstrap ./functions/bridge 
node ./scripts/build.mjs

mkdir -p ./dist/python-runtime/
cp ./functions/python-runtime/index.py ./dist/python-runtime/index.py

mkdir -p ./dist/dockerfiles/
cp ./functions/docker/python.Dockerfile ./dist/dockerfiles/python.Dockerfile

cd ./support/bridge-task
timestamp=$(date +%Y%m%d%H%M%S)
docker buildx create --name multi --driver docker-container --use
docker buildx build --platform linux/amd64,linux/arm64 -t ghcr.io/sst/sst/bridge-task:$timestamp -t ghcr.io/sst/sst/bridge-task:latest $([ "$DOCKER_PUSH" = "true" ] && echo "--push") .


cd $ORIGINAL_DIR
